#pragma Once
#include "BlockQueue.hpp"
#include <vector>
#include <string>
#include <ctime>
#include "Task.hpp"

// std::string opers = "+-*/%";
void *Consumer(void *args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(args);
    while (true)
    {
        // 接收任务
        Task data = bq->pop();
        usleep(10000);

        // 处理任务
        data.run();
        cout << endl;
        cout << "处理了任务 " << data.GetTask() << data.GetResult();
    }

    return nullptr;
}

void *Producer(void *args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(args);
    while (true)
    {
        // 产生任务
        char op = opers[rand() % opers.size()];
        int data1 = rand() % 20 + 1;
        int data2 = rand() % 10;
        usleep(10);
        Task t(data1, data2, op);

        // 发送任务
        bq->push(t);
        cout << endl;
        cout << "产生了一个任务=> " << t.GetTask() << "??";
        sleep(1);
    }
    return nullptr;
}

int main()
{
    srand(time(nullptr));

    BlockQueue<Task> *bq = new BlockQueue<Task>(5); //阻塞队列

    //创造单生产者 单消费者
    pthread_t c, p;
    pthread_create(&c, nullptr, Producer, bq);
    pthread_create(&p, nullptr, Consumer, bq);

    pthread_join(p, nullptr);
    pthread_join(c, nullptr);

    delete bq;
    return 0;
}
